"""Update DataTableOwnership

Revision ID: 8e3a8aa02aa7
Revises: dc68ab1e12b9
Create Date: 2020-08-26 23:01:04.912894

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "8e3a8aa02aa7"
down_revision = "dc68ab1e12b9"
branch_labels = None
depends_on = None


def upgrade():
    conn = op.get_bind()
    data_table_ownership_data_table_id_constraint = "data_table_id"
    if conn.dialect.name == "postgresql":
        data_table_ownership_data_table_id_constraint = (
            "data_table_ownership_data_table_id_key"
        )

    # ### commands auto generated by Alembic - please adjust! ###
    ownership_table = sa.Table(
        "data_table_ownership", sa.MetaData(op.get_bind()), autoload=True
    )

    for constraint in ownership_table.foreign_key_constraints:
        # The only foreign key constraint should be the constraint of
        # data_table_id to "data_table.id"
        op.drop_constraint(constraint.name, "data_table_ownership", type_="foreignkey")

    op.drop_constraint(
        data_table_ownership_data_table_id_constraint,
        "data_table_ownership",
        type_="unique",
    )

    op.alter_column(
        "data_table_ownership",
        "data_table_id",
        existing_type=sa.Integer(),
        nullable=False,
    )

    op.create_foreign_key(
        "data_table_ownership_data_table_id_fk",
        "data_table_ownership",
        "data_table",
        ["data_table_id"],
        ["id"],
        ondelete="CASCADE",
    )

    op.add_column(
        "data_table_ownership", sa.Column("uid", sa.Integer(), nullable=False)
    )
    op.create_foreign_key(
        "data_table_ownership_uid_fk",
        "data_table_ownership",
        "user",
        ["uid"],
        ["id"],
        ondelete="CASCADE",
    )
    op.create_unique_constraint(
        "unique_table_ownership", "data_table_ownership", ["data_table_id", "uid"]
    )

    op.drop_column("data_table_ownership", "owner")
    # ### end Alembic commands ###


def downgrade():
    conn = op.get_bind()
    data_table_ownership_data_table_id_constraint = "data_table_id"
    if conn.dialect.name == "postgresql":
        data_table_ownership_data_table_id_constraint = (
            "data_table_ownership_data_table_id_key"
        )
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "data_table_ownership",
        sa.Column("owner", sa.String(length=255), nullable=True),
    )
    op.drop_constraint(
        "data_table_ownership_uid_fk", "data_table_ownership", type_="foreignkey"
    )
    op.drop_constraint(
        "data_table_ownership_data_table_id_fk",
        "data_table_ownership",
        type_="foreignkey",
    )
    op.drop_constraint("unique_table_ownership", "data_table_ownership", type_="unique")
    op.drop_column("data_table_ownership", "uid")

    op.alter_column(
        "data_table_ownership",
        "data_table_id",
        existing_type=sa.Integer(),
        nullable=True,
    )
    op.create_foreign_key(
        "data_table_ownership_data_table_id_fk",
        "data_table_ownership",
        "data_table",
        ["data_table_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.create_unique_constraint(
        data_table_ownership_data_table_id_constraint,
        "data_table_ownership",
        ["data_table_id"],
    )
    # ### end Alembic commands ###
